package la.iok.hzsvn.lewin.movie.cloud.controller;

import la.iok.hzsvn.lewin.movie.cloud.model.TokenInfo;
import la.iok.hzsvn.lewin.movie.cloud.model.WxLoginParam;
import la.iok.hzsvn.lewin.movie.cloud.model.WxQRCodeParam;
import la.iok.hzsvn.lewin.movie.cloud.service.WxInvoker;
import la.iok.hzsvn.lewin.movie.cloud.service.WxRestService;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("wx")
public class WxController {
    private final WxInvoker wxInvoker;
    private final WxRestService wxRestService;

    public WxController(WxInvoker wxInvoker, WxRestService wxRestService) {
        this.wxInvoker = wxInvoker;
        this.wxRestService = wxRestService;
    }

    /**
     * 小程序登录
     * @return 登录令牌
     */
    @PostMapping("token")
    public SingleDataResponse<TokenInfo> miniLogin(@RequestBody WxLoginParam param){
        return wxInvoker.code2Session(param.getCode(),param.getDevice(),param.getClient());
    }

    @PostMapping("phone/token")
    public SingleDataResponse<TokenInfo> miniPhoneLogin(@RequestBody WxLoginParam param){
        return new SingleDataResponse<>(wxInvoker.phoneNumber(param));
    }

    @PostMapping("qrcode")
    public void qrCode(HttpServletResponse response, @RequestBody WxQRCodeParam param){
        wxRestService.wxCode(response, param);
    }
}
